﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Linq;
using System.Drawing.Drawing2D;
using XbdSoft.Contract;
using XbdSoft.Client;
using System.Configuration;

namespace XbdSoft
{
    public partial class XbdForm : Form
    {

        #region Default Dock Content
        protected readonly List<KeyValuePair<Type, string>> DefaultOpenContents = new List<KeyValuePair<Type, string>>() { 
            new KeyValuePair<Type,string>(typeof(XbdInfoForm),"info")
        
        };
        #endregion

        #region Ctor
        public XbdForm()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            NotifyCloseToolStripMenuItem.FillWithGradient(Color.LightSteelBlue, Color.LightBlue, LinearGradientMode.Vertical);
            this.FillWithGradient(Color.LightSteelBlue, Color.LightBlue, LinearGradientMode.Vertical);
            MainStatusStrip.FillWithGradient(Color.LightSteelBlue, Color.LightBlue, LinearGradientMode.Vertical);
            MainMenuStrip.FillWithGradient(Color.LightSteelBlue, Color.LightBlue, LinearGradientMode.Vertical);
             
        } 
        #endregion
        

        #region Dock Panel Header Content Menu Events
        private void CloseDockPanelHeaderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockContent content = MainDockPanel.Contents.FirstOrDefault(c =>
            {
                DockContent con = c as DockContent;
                return con.IsActivated;

            }) as DockContent;

            if (content != null)
            {
                var isDefaultOpenContent = DefaultOpenContents.Any(kv => kv.Key == content.GetType() && kv.Value.Equals(content.Name, StringComparison.OrdinalIgnoreCase));
                if (!isDefaultOpenContent)
                {
                    content.Close();
                }
            }
        }
        
        #endregion

        #region Assist Method

        private T OpenDockForm<T>(string name,string tabText=null)
            where T : DockContent, new()
        {
            T content = MainDockPanel.Contents.FirstOrDefault(c =>
            {
                DockContent con = c as DockContent;
                return c.GetType() == typeof(T) && con.Name == name;
            }) as T;
            if (content == null)
            {
                content = new T();
                content.DockPanel = MainDockPanel;
                content.Dock = DockStyle.Fill;
                content.DockAreas = DockAreas.Document;
                content.TabPageContextMenuStrip = DockPanelHeaderContextMenuStrip;
                content.Name = name;
            }

            if (!String.IsNullOrEmpty(tabText))
            {
                content.TabText = tabText;
            }
            try
            {
                content.Show();
                content.Activate();
            }
            catch { }
            return content;
        }


        #endregion

        #region Form Load
        private void XbdForm_Load(object sender, EventArgs e)
        {
            var loginForm = new XbdLoginForm();
            var result = loginForm.ShowDialog();
            if (loginForm.HasLogin)
            {
                var infoForm = OpenDockForm<XbdInfoForm>("Info");
            }
            else
            {
                Close();
            }
            //var infoForm = OpenDockForm<XbdInfoForm>("Info");
            OpenDockForm<XbdMP3RecDownload>("Mp3RecDownload");

        }
        #endregion

        #region MenuItems Events
        private void MP3InfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XbdInfoForm infoForm = OpenDockForm<XbdInfoForm>("Info");
        }

        private void MP3CustomizedDowmloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var mp3CustDownloadForm = OpenDockForm<XbdMP3CustDownload>("Mp3CustDownload");
        }
       

        private void MP3DownLoadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var mp3DownloadForm = OpenDockForm<XbdMP3RecDownload>("Mp3RecDownload");
        }
        #endregion

        #region Notify Icon
        private void XbdForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            { 
                MainNotifyIcon.Visible = true;
                Visible = false;
            }
        }
        #endregion

        #region Notify Icon Event
        private void MainNotifyIcon_Click(object sender, EventArgs e)
        {
        }
        private void MainNotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized;
            MainNotifyIcon.Visible = false;
            Visible = true;
        }
        #endregion

        #region notify menu item
        private void NotifyCloseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        private void DockPanelHeaderContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            CloseDockPanelHeaderToolStripMenuItem.Visible = true;
            var content = (DockContent)MainDockPanel.ActiveContent;
            if (content != null)
            {
                var isDefaultOpenContent = DefaultOpenContents.Any(kv => kv.Key == content.GetType() && kv.Value.Equals(content.Name, StringComparison.OrdinalIgnoreCase));
                if (isDefaultOpenContent)
                {
                    CloseDockPanelHeaderToolStripMenuItem.Visible = false;
                    if (DockPanelHeaderContextMenuStrip.Items.Count == 1)
                    {
                        e.Cancel = true;
                    }
                } 
            }
        }

        private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var setting = new XbdSettingsForm();
            setting.ReloadSettings();
            setting.ShowDialog();
        }

        private void LogOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }


      
        
                
    }

    public static class ControlExtension
    {
        public static void FillWithGradient(this Control control,Color color1,Color color2,LinearGradientMode mode)
        {
            Rectangle rectangle = control.ClientRectangle;
            if (rectangle.Width > 0 && rectangle.Height > 0)
            {
                Bitmap bitMap = new Bitmap(rectangle.Width, rectangle.Height);
                Graphics g = Graphics.FromImage(bitMap);
                LinearGradientBrush brush = new LinearGradientBrush(rectangle, color1, color2, mode);
                g.FillRectangle(brush, rectangle);
                control.BackgroundImage = bitMap;
            }
        }
        public static void FillWithGradient(this ToolStripMenuItem control, Color color1, Color color2, LinearGradientMode mode)
        {
            Rectangle rectangle = control.Bounds;
            Bitmap bitMap = new Bitmap(rectangle.Width, rectangle.Height);
            Graphics g = Graphics.FromImage(bitMap);
            LinearGradientBrush brush = new LinearGradientBrush(rectangle, color1, color2, mode);
            g.FillRectangle(brush, rectangle);
            control.BackgroundImage = bitMap;

        }
    }
}
